package com.eco.fanliapp.service.impl;

import com.alibaba.fastjson.JSON;
import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.CountLauncher;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IJobOrderInfoGetService;
import com.eco.fanliapp.service.ITaoBaoOrderService;
import com.eco.fanliapp.utils.DateCalculateUtils;
import com.eco.fanliapp.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class JobOrderInfoGetServiceImpl implements IJobOrderInfoGetService {
    private static final Logger log = LoggerFactory.getLogger(JobOrderInfoGetServiceImpl.class);

    @Autowired
    private ITaoBaoOrderService taobaoOrderService;

    @Autowired
    private OrderTaskUtils orderTaskUtils;

    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 计数器类对象实例
     */
    private CountLauncher countLauncher;

    public CountLauncher getCountLauncher() {
        return countLauncher;
    }

    public void setCountLauncher(CountLauncher countLauncher) {
        this.countLauncher = countLauncher;
    }
    public void handleOrder(String startTime,String endTime) {
//        String timeFlag ="";
        //当前时间
        long startMillions = System.currentTimeMillis();
        log.info("handleOrder：{}",startTime);
        try {
            Long tkStatus = 12L;//淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
            Long orderCountType = 2L;//member_type   推广者角色类型,2:二方，3:三方，不传，表示所有角色
            String orderQueryType = "1";//查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
//          Long orderScene = 2L;//场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1

            //获取淘宝联盟账号-排除已过期的
            List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();
            log.info("*********等待异步方法执行完毕-付款订单定时任务qwe" + "--startTime=" + startTime + "--endTime=" + endTime+ JSON.toJSONString(taobaoSchemes));

            if(taobaoSchemes.size() > 0) {
                for (int i = 2; i <= 3; i++) {
//                    CountLauncherPay countLauncherPay = new CountLauncherPay(taobaoSchemes.size());
//
//                    taobaoOrderService.setCountLauncherPay(countLauncherPay);
                    log.info("taobaoSchemes：{}",i);
                    Long pageNo = 1L;
                    for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                        log.info("taobaoOrderService：{}",taobaoScheme2.getAppSession());
                        taobaoOrderService.getPayOrderDetail(
                                taobaoScheme2.getAppSession(), startTime ,endTime, pageNo, tkStatus,
                                orderCountType, orderQueryType, (long)i,1,null);
                        log.info("当前线程睡眠100毫秒！");
                        try {
                            Thread.sleep(100 * 1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    log.info("联盟拉取订单循环结束");

                    while (true) {
                        if ((System.currentTimeMillis() - startMillions) > 1000 * 35) {
                            break;
                        }
                        log.info("*********等待异步方法执行完毕-付款订单定时任务111" +  "--countLauncherPay="  + "--time=" + System.currentTimeMillis());
                        try {
                            Thread.sleep(2000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
//            if ((System.currentTimeMillis() - startMillions) < 1000 * 30 ) {
//                try {
//                    Thread.sleep(1000 * 20);//主线程等待，保证子线程执行完毕
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }

            tkStatus = 3L;//淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
            orderQueryType = "3";//查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
            if(taobaoSchemes.size() > 0) {
                for (int i = 2; i <= 3; i++) {
//                    CountLauncherSuccess countLauncherSuccess = new CountLauncherSuccess(taobaoSchemes.size());
//                    taobaoOrderService.setCountLauncherSuccess(countLauncherSuccess);

                    Long pageNo = 1L;
                    for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                        taobaoOrderService.getSettleOrderDetail(taobaoScheme2.getAppSession(), startTime, endTime, pageNo, tkStatus,
                                orderCountType, orderQueryType, (long)i,1,null);

                        try {
                            Thread.sleep(100 * 1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    while (true) {
                        if ((System.currentTimeMillis() - startMillions) > 1000 * 35) {
                            break;
                        }
                        log.info("*********等待异步方法执行完毕-结算订单定时任务111" +  "--countLauncherSuccess=" + "--time=" + System.currentTimeMillis());
                        try {
                            Thread.sleep(5000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }
            }

//            if ((System.currentTimeMillis() - startMillions) < 1000 * 30 ) {
//                try {
//                    Thread.sleep(1000 * 20);//主线程等待，保证子线程执行完毕
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }


        } catch (Exception e) {
            log.error("24小时付款订单定时任务错误---message付款订单-暂无数据i=");
        }finally {
            log.info("结束24小时付款订单定时任务" + (System.currentTimeMillis() - startMillions));
        }
    }


    /**
     * 处理时间
     * @param timeFlag
     * @return
     */
    public List<String> handleTime(String timeFlag){
        //当前日期参照点
        Date currentTime = new Date();
        String currentTimeStr = DateUtils.date2Str(currentTime, null);
        if(timeFlag==null || "".equals(timeFlag)){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentTime);
            calendar.add(Calendar.MINUTE, -(24 * 60 + 20));

            timeFlag = DateUtils.date2Str(calendar.getTime(), null);
            log.info("开始时间参照点{}",timeFlag);
            redisTemplate.opsForValue().set("timeFlag",calendar.getTime().getTime()+"");
        }
        //切割时间段(交易类型 M/D/H/N -->每月/每天/每小时/每分钟,多久分成一份，开始时间，结束时间)
        List<String> list = DateCalculateUtils.cutDate("N",1200, timeFlag, currentTimeStr);
        return list;
    }

}
